package com.jing.weibo.controller.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import weibo4j.model.WeiboException;

import com.jing.system.comm.BaseController;
import com.jing.system.comm.contants.DataCons;
import com.jing.system.comm.contants.StringCons;
import com.jing.system.comm.contants.WeiboCons;
import com.jing.system.comm.enums.RuleType;
import com.jing.system.utils.PointCurrencyUtil;
import com.jing.weibo.pojo.system.Feedback;
import com.jing.weibo.pojo.system.Rule;
import com.jing.weibo.pojo.user.UserInfo;
import com.jing.weibo.service.system.FeedbackService;
import com.jing.weibo.service.system.StationletterService;
import com.jing.weibo.service.user.UserInfoService;

/**
 * 反馈建议的Controller
 * @author jing.yue
 * @version 2012/09/11 1.0.0
 */
@Controller
public class FeedbackController extends BaseController {

	private static final Logger logger = Logger.getLogger(FeedbackController.class);

	@Autowired
	private FeedbackService feedbackService;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private StationletterService stationletterService;

	/**
	 * 添加反馈建议
	 * @return
	 */
	@RequestMapping(value = "/feedback/json/addFeedback")
	@ResponseBody
	public ModelMap addFeedback(HttpServletRequest request, Feedback feedback) {
		String result = null;
		StringBuffer errorWeiboMsg = new StringBuffer();
		UserInfo userInfo = getSessionUser(request);
		try {
			if(userInfo == null) {
				result = ERROR_NOT_LOGIN;
			} else {
				feedback.setAdduser(userInfo.getUsername());
				feedback.setReceiveusername(WeiboCons.system_weiboId);
				feedbackService.saveFeedback(feedback);
				result = SUCCESS;
			}
		} catch (Exception e) {
			result = ERROR;
			logger.error(userInfo.getNickname() + ": 反馈建议异常!", e);
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("result", result);
		modelMap.put("errorWeiboMsg", errorWeiboMsg.toString());
		return modelMap;
	}

	/**
	 * 审核反馈建议
	 * @return
	 */
	@RequestMapping(value = "/feedback/json/checkFeedback")
	@ResponseBody
	public ModelMap checkFeedback(HttpServletRequest request, Feedback feedback) {
		String result = null;
		UserInfo userInfo = getSessionUser(request);
		try {
			//处理不是开发者
			if(!WeiboCons.system_weiboId.equals(userInfo.getUsername())) {
				result = ERROR_NOT_DEVELOPER;
			}
			else {
				feedbackService.updateCheckFeedback(feedback);

				feedback = feedbackService.findByIdFeedback(feedback.getId());

				//反馈一个有建议性的意见[并且不为匿名反馈]
				if(feedback.getIscheck().intValue() == 1 && !StringCons.USERNAME_ANONYMITY.equals(feedback.getAdduser().trim())) {
					PointCurrencyUtil pointCurrencyUtil = new PointCurrencyUtil(RuleType.TYPE_FEEDBACK_IDEA, feedback.getAdduser());
					pointCurrencyUtil.handlePointCurrency();
					//同步Session中的用户信息
					setSessionUser(request, userInfoService.findByUserName(userInfo.getUsername()));

					Rule rule = DataCons.ruleMap.get(RuleType.TYPE_FEEDBACK_IDEA.getCode());
					String content = String.format(StringCons.STATIONLETTER_CHECK_FEEDBACK_CONTENT, feedback.getContent(), rule.getCurrency(), rule.getPoint());
					//发送站内信通知
					stationletterService.saveStationletter(StringCons.STATIONLETTER_CHECK_FEEDBACK_TITLE, content, feedback.getAdduser(), WeiboCons.system_weiboId);
				}
				result = SUCCESS;
			}
		} catch (Exception e) {
			result = ERROR;
			logger.error(userInfo.getNickname() + ": 审核反馈建议异常!", e);
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("result", result);
		return modelMap;
	}

	/**
	 * 跳转到反馈列表页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/toFeedbackList")
	public String toFeedbackList(HttpServletRequest request) {
		UserInfo userInfo = getSessionUser(request);
		//不为管理员
		if(userInfo.getIsadmin().intValue() != 1) {
			logger.error(userInfo.getNickname() + ": 您不是管理员!");
			return ERROR_ADMIN;
		}
		return "admin/feedback/feedback_list";
	}

	/**
	 * 管理员获取反馈列表
	 * @return
	 */
	@RequestMapping(value = "/feedback/json/findFeedback")
	@ResponseBody
	public ModelMap findFeedback(HttpServletRequest request, Feedback feedback) {
		String result = null;
		String weiboMsg = null;
		Map<String, Object> map = null;
		UserInfo userInfo = getSessionUser(request);
		//不为管理员
		if(userInfo.getIsadmin().intValue() != 1) {
			logger.error(userInfo.getNickname() + ": 您不是管理员!");
			result = ERROR;
		}
		else {
			try {
				//设置接收用户
				//feedback.setReceiveusername(userInfo.getUsername());
				map = feedbackService.findFeedback(feedback);
				result = SUCCESS;
			} catch (WeiboException e) {
				logger.error(WeiboCons.getErrorMsg(e));
				result = ERROR_WEIBO;
				weiboMsg = WeiboCons.getErrorMsg(e);
			} catch (Exception e) {
				logger.error(userInfo.getNickname() + ": 获取单向粉丝信息异常!", e);
				result = ERROR;
			}
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("data", map);
		modelMap.put("result", result);
		modelMap.put("weiboMsg", weiboMsg);
		//处理是否为开发者
		if(WeiboCons.system_weiboId.equals(userInfo.getUsername())) {
			modelMap.put("isDeveloperWeiboId", "true");
		}
		return modelMap;
	}

}